import { BaseURL } from '../../app.js';


Page({

  data: {
    phone: '',
    wifiList: [],
    accountNumber: 'Citydo', //Wi-Fi 的SSID，即账号
    bssid: '48:bd:3d:d9:d6:a0', //Wi-Fi 的ISSID
    password: 'cWfHvj@Gtd89' //Wi-Fi 的密码
  },
  onLoad(){

    var phoneValue = wx.getStorageSync('phone');

    this.setData({
      phone: phoneValue
    })

    console.log('-----door-control------' + this.data.phone);

  },

  onUnload() {
    // this.stopSearch()
  },

  openDoor() {

    if(!this.data.phone){
      this.setData({
        hiddenmodalput: false
      })
      return;
    }

    this.connected();
  },

  connected: function() {

    var that = this

    wx.startWifi({
      success: function() {

        wx.connectWifi({

          SSID: that.data.accountNumber,

          BSSID: that.data.bssid,

          password: that.data.password,

          success: function(res) {

            wx.showToast({

              title: 'wifi连接成功',

            })
             that.openDoorRequest();
          },

          fail: function(res) {

            wx.showToast({

              title: 'wifi连接失败',

            })
          }
        })

      },
      fail(err) {
        console.error(err)
      }
    })
  },

  openDoorRequest() {
    wx.request({
      method: 'POST',
      url: `${BaseURL}/safe/attendance/openDoor`,
      data: {
        phone: this.data.phone
      },
      header: {
        'content-type': 'application/json'
      },
      success(res){

        var aa = res.data;
        console.log("=====",res);
        if (res.data.code == 0) {
          console.log("==res.data.code===", res.data.code);

          wx.showToast({
            title: '开门成功',
          })
        }else {
          wx.showToast({
            title: res.msg,
          })
          console.log("=====", res.msg);
        }
      }
    })
  }

})